﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.SafeManage.PatrolPerson;
using LJX.SmartPark.Domain.SafeManage.PatrolRecord;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LJX.SmartPark.Service.SafeManage.PatrolPersonServeice;

namespace LJX.SmartPark.Service.SafeManage
{
    public class PatrolRecordServeice:IPatrolRecordServeice
    {
        private readonly IRepository<PatrolRecord> _patrolRecordService;

        public PatrolRecordServeice(IRepository<PatrolRecord> patrolRecordService)
        {
            _patrolRecordService = patrolRecordService;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeletePatrolRecordAsync(PatrolRecord input)
        {
            var result = await _patrolRecordService.DeleteByIdAsync(input.MapTo<PatrolRecord>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _patrolRecordService.GetCount();
        }

        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="fy3"></param>
        /// <returns></returns>
        public async Task<Response<List<PatrolRecord>>> GetPatrolRecordAsync(Fy3 fy3)
        {
            var students = await _patrolRecordService.GetAllListAsync();
            var result = students.MapToList<PatrolRecord>();

            if (!string.IsNullOrEmpty(fy3.patrolplanName))
            {
                result = result.Where(a => a.PatrolplanName.Contains(fy3.patrolplanName)).ToList();
            }

            var totalCount = result.Count();
            result = result.Skip(((fy3.pageIndex - 1) * fy3.pageSize)).Take(fy3.pageSize).ToList();
            return Response<List<PatrolRecord>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<PatrolRecord> GetPatrolRecordById(PatrolRecord input)
        {
            var patrolRecord = _patrolRecordService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = patrolRecord.MapTo<PatrolRecord>();
            return Response<PatrolRecord>.Ok(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PostAddPatrolRecordAsync(PatrolRecord input)
        {
            var result = _patrolRecordService.InsertAsyncById(input.MapTo<PatrolRecord>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdPatrolRecordAsync(PatrolRecord input)
        {
            var result = _patrolRecordService.UpdateAsync(input.MapTo<PatrolRecord>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        public class Fy3
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string? patrolplanName { get; set; }
        }
    }
}
